<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">退款来源：</span>
                <el-select v-model="refund_from" clearable placeholder="请选择类型">
                    <el-option label="取消订单" value="cancel" />
                    <el-option label="售后退款" value="refund" />
                    <el-option label="商品降级" value="product_demotion" />
                    <el-option label="取消商品" value="product_cancel" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">退款状态：</span>
                <el-select v-model="refund_status" clearable placeholder="请选择类型">
                    <el-option label="退款中" value="1" />
                    <el-option label="退款成功" value="2" />
                    <el-option label="退款失败" value="3" />
                    <el-option label="退款余额" value="4" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">订单编号：</span>
                <el-input placeholder="输入订单编号" v-model.trim="order_sn" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">转账时间：</span>
                <el-date-picker
                    v-model="area_time"
                    type="daterange"
                    unlink-panels
                    value-format="YYYY-MM-DD"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                />
            </div>
            <div class="task-one__btn task-wrap-btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
                <el-button @click="clickExport">导出</el-button>
            </div>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <el-table-column prop="refund_no" label="退款编号" min-width="120" />
            <el-table-column prop="order_sn" label="订单编号" min-width="120" />
            <el-table-column prop="real_refund_amount" label="退款金额" min-width="120" />
            <el-table-column label="退款状态" min-width="120">
                <template #default="{ row }">
                    <span style="color: #4b93fa">{{ row.refund_status_label }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单时间" min-width="240">
                <template #default="{ row }">
                    <div v-if="row.admin_time">退款申请:{{ row.admin_time_label }}</div>
                    <div v-if="row.pay_time">退款成功:{{ row.pay_time_label }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="refund_reason" label="失败原因" min-width="220" />
            <el-table-column label="操作" width="130" class-name="operation-nav" fixed="right">
                <template #default="{ row }">
                    <el-button link type="primary" class="theme-btn" @click="clickBacks(row)" v-if="row.refund_status == 3">退回至余额</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch } from "vue"
import { Search } from "@element-plus/icons-vue"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, search_btn, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
        order_sn: order_sn.value,
        s_time: area_time.value ? area_time.value[0] : "",
        l_time: area_time.value ? area_time.value[1] : "",
        refund_from: refund_from.value,
        refund_status: refund_status.value,
    }
    getdata("finance/log/refundItem", params)
}

/**
 * 搜索
 * @param {String} order_sn 订单编号
 * @param {String} area_time 对账日期
 * @param {String} refund_from 退款来源[cancel:取消订单,refund:售后退款,product_demotion:商品降级,product_cancel:取消]
 * @param {String} refund_status 退款状态[1:退款中,2:退款成功,3:退款失败,4:退款余额]
 */
const order_sn = ref("")
const area_time = ref("")
const refund_from = ref("")
const refund_status = ref("")
const handleSearch = () => {
    search_btn.value = true
    pubRefreshParams(getList)
}

// 导出
const clickExport = () => {
    proxy.$axios
        .get("finance/log/refundItemExport", {
            params: {
                order_sn: order_sn.value,
                s_time: area_time.value ? area_time.value[0] : "",
                l_time: area_time.value ? area_time.value[1] : "",
                refund_from: refund_from.value,
                refund_status: refund_status.value,
            },
            responseType: "blob",
        })
        .then(function (res) {
            let contentDisposition = res.headers["content-disposition"]
            let filename = decodeURI(contentDisposition.split("fileName=")[1] || contentDisposition.split("filename=")[1])
            let blob = new Blob([res.data], { type: res.headers["content-type"] })
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement("a")
            link.href = url
            link.download = filename
            document.body.appendChild(link)
            link.click()
            URL.revokeObjectURL(url) // 释放内存
            document.body.removeChild(link)
        })
        .catch((err) => {})
}

// 退回至余额
const clickBacks = proxy.$util.throttle((row) => {
    proxy.$axios
        .post("finance/log/refundReject", {
            refund_id: row.refund_id,
        })
        .then((res) => {
            if (res.data.code == 0) {
                proxy.$message({ message: "退回成功", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}, 500)
</script>
<script>
export default {
    name: "RefundDetail",
}
</script>

<style lang="less" scoped></style>
